﻿using System.Collections.Generic;
using System.Linq;

namespace Tuxedo.Compile
{
	internal class WhileLoop : CodeLine
	{
		private Expression condition;
		private CodeLine[] loopCode;

		public WhileLoop(Expression condition, IList<CodeLine> loopCode)
		{
			this.condition = condition;
			this.loopCode = loopCode.ToArray();
		}

		public static CodeLine Parse(TokenStream tokens)
		{
			tokens.PopExpected("while");
			tokens.PopExpected("(");

			Expression condition = Expression.ParseExpression(tokens);

			tokens.PopExpected(")");

			IList<CodeLine> loopCode = CodeLine.ParseBlock(tokens, true);

			return new WhileLoop(condition, loopCode);
		}
	}
}
